package com.kinghunt.misp.web.controller.devicex;

import com.kinghunt.misp.common.UploadFileConfig;
import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import com.bygoodtech.kings.common.utils.Checker;
import com.kinghunt.misp.common.dto.devicex.DeviceDTO;
import com.kinghunt.misp.common.dto.userx.UserDTO;
import com.kinghunt.misp.common.enums.devicex.DeviceTypeEnum;
import com.kinghunt.misp.common.param.devicex.DeviceParam;
import com.kinghunt.misp.common.utils.UploadUtils;
import com.kinghunt.misp.service.api.devicex.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Objects;

import static com.kinghunt.misp.web.context.SystemContext.getCurrentUser;

/**
 * 设备管理
 *
 * @Author ciaran
 * @Date 2023/7/13 21:58l
 */
@RestController
@RequestMapping("/api/device/")
public class DeviceController {
    @Resource
    UploadFileConfig uploadFileConfig;

    @Autowired(required = false)
    private DeviceService deviceService;

    /**
     * 列表显示：显示设备列表
     */
    @GetMapping("getDeviceList")
    public ApiResult<PageInfo<DeviceDTO>> getDeviceList(DeviceParam deviceParam) {
        //UserDTO loginUser = getCurrentUser();
        return deviceService.getDeviceList(deviceParam);
    }
    /**
     * 设备详情展示
     */
    @GetMapping("getDeviceDetail")
    public ApiResult<DeviceDTO> getDeviceDetail(Long deviceId) {
        if(Objects.isNull(deviceId)){
            return ApiResult.failOfMessage("查询失败，请输入设备信息！");
        }
        return deviceService.getDeviceDetail(deviceId);
    }
    /**
     * 设备登记
     */
    @PostMapping(value = "addDevice", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult addDevice(@Valid @RequestBody DeviceParam deviceParam) {
        if(Objects.isNull(deviceParam.getDeviceNo())){
            return ApiResult.failOfMessage("新增失败，请输入设备编号！");
        }
        UserDTO loginUser = getCurrentUser();
        deviceParam.setCreatorId(loginUser.getId());
        deviceParam.setUpdaterId(loginUser.getId());
        return deviceService.addDevice(deviceParam);
    }
    /**
     * 修改设备
     */
    @PostMapping(value = "updateDevice", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult updateDevice(@Valid @RequestBody DeviceParam deviceParam) {
        UserDTO loginUser = getCurrentUser();
        deviceParam.setUpdaterId(loginUser.getId());
        return deviceService.updateDevice(deviceParam);
    }
    /**
     * 删除设备
     *
     * @param deviceId
     * @return 是否成功
     */
    @DeleteMapping("deleteDevice")
    public ApiResult<Boolean> deleteDevice(Long deviceId) {
        if(Objects.isNull(deviceId)){
            return ApiResult.failOfMessage("查询失败，请输入设备信息！");
        }
        return deviceService.deleteDevice(deviceId);
    }

    /**
     * 启动设备
     *
     * @param deviceId
     * @return 是否成功
     */
    @PostMapping(value = "enableDevice", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult<Boolean> enableDevice(Long deviceId) {
        if(Objects.isNull(deviceId)){
            return ApiResult.failOfMessage("查询失败，请输入设备信息！");
        }
        DeviceParam deviceParam = new DeviceParam();
        deviceParam.setDeviceId(deviceId);
        deviceParam.setStatus(DeviceTypeEnum.DEVICE_TYPE_1.getValue());
        return deviceService.updateDevice(deviceParam);
    }
    /**
     * 停止设备
     *
     * @param deviceId
     * @return 是否成功
     */
    @PostMapping(value = "disableDevice", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult<Boolean> disableDevice(Long deviceId) {
        if(Objects.isNull(deviceId)){
            return ApiResult.failOfMessage("查询失败，请输入设备信息！");
        }
        DeviceParam deviceParam = new DeviceParam();
        deviceParam.setDeviceId(deviceId);
        deviceParam.setStatus(DeviceTypeEnum.DEVICE_TYPE_2.getValue());
        return deviceService.updateDevice(deviceParam);
    }
    /**
     * 设备报废
     *
     * @param deviceId
     * @return 是否成功
     */
    @PostMapping(value = "scrapDevice", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult<Boolean> scrapDevice(Long deviceId) {
        if(Objects.isNull(deviceId)){
            return ApiResult.failOfMessage("查询失败，请输入设备信息！");
        }
        DeviceParam deviceParam = new DeviceParam();
        deviceParam.setDeviceId(deviceId);
        deviceParam.setStatus(DeviceTypeEnum.DEVICE_TYPE_5.getValue());
        return deviceService.updateDevice(deviceParam);
    }
    /**
     * 上传设备图片
     *
     * @param file    图片文件
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping(value = "uploadDeviceImage")
    @ResponseBody
    public ApiResult<String> uploadDeviceImage(@RequestParam("file") MultipartFile file,
                                            HttpServletRequest request) {
        UserDTO loginUser = getCurrentUser();
        //获取绝对路径
        String realPath = uploadFileConfig.getDeviceImagePath();
        String uploadResult = Checker.success(UploadUtils.uploadImage(loginUser, file, request,realPath));
        return ApiResult.ok(uploadResult);
    }
}
